package com.barneyx.mshinfoplatform.web.admin.admin;

import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.entity.admin.AdminPermissionEntity;
import com.barneyx.mshinfoplatform.entity.admin.AdminRoleEntity;
import com.barneyx.mshinfoplatform.exception.AdminUserException;
import com.barneyx.mshinfoplatform.util.admin.AdminUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/account/permission/")
@Slf4j
public class AdminPermissionController {
    @Resource
    private AdminUtil adminUtil;

    @GetMapping("list")
    public ExResult<?> getPermissionList() {
        return ExResult.ofAuto(adminUtil.getPermissionList());
    }

    @GetMapping("list-level-2")
    public ExResult<?> getPermissionLevel2() {
        return ExResult.ofAuto(adminUtil.getPermissionLevel2());
    }

    @GetMapping("tree-list")
    public ExResult<?> getRoleTreeList() {
        List<AdminPermissionEntity> roles = adminUtil.getPermissionList();
        Map<String, AdminPermissionEntity> menuMap = new HashMap<>();
        for (AdminPermissionEntity menu : roles) {
            menuMap.put(menu.getPermissionId(), menu);
        }
        // 寻找自己的父节点
        for (AdminPermissionEntity menu : roles) {
            String parentId = menu.getPermissionParentId();
            if (parentId.equals("0")) {
                continue;
            }
            // 取map中当前菜单的上级菜单
            AdminPermissionEntity parentMenu = menuMap.get(parentId);
            // 上级菜单中的子菜单
            List<AdminPermissionEntity> childMenuList = parentMenu.getChildren();
            // 子菜单为空
            if (CollectionUtils.isEmpty(childMenuList)) {
                childMenuList = new ArrayList<>();
                parentMenu.setChildren(childMenuList);
            }
            childMenuList.add(menu);
        }
        // 找到根节点输出
        List<AdminPermissionEntity> result = new ArrayList<>();
        for (AdminPermissionEntity menu : roles) {
            if (menu.getPermissionParentId().equals("0")) {
                result.add(menu);
            }
        }

        return ExResult.ofAuto(result);
    }


    @PutMapping("update")
    public ExResult<?> updatePermissionById(@RequestBody AdminPermissionEntity permission) throws AdminUserException {
        return ExResult.ofAuto(adminUtil.updatePermissionById(permission));
    }

    @PostMapping("create")
    public ExResult<?> createPermission(@RequestBody AdminPermissionEntity permission) throws AdminUserException {
        return ExResult.ofAuto(adminUtil.createPermission(permission));
    }

    @GetMapping("get-by-id")
    public ExResult<?> getPermissionById(@RequestParam("id") String permissionId) {
        return ExResult.ofAuto(adminUtil.getPermissionById(permissionId));
    }

    @DeleteMapping("delete-by-id")
    public ExResult<?> deletePermissionById(@RequestParam("id") String Permissionid) {
        return ExResult.ofAuto(adminUtil.deletePermissionById(Permissionid));
    }


    /**
     * 获取指定分组编号下面的所有权限列表
     *
     * @param roleId
     * @return
     */
    @GetMapping("role-permission-list")
    public ExResult<?> getPermissionListByRoleId(@RequestParam("roleid") String roleId) {
        return ExResult.ofAuto(adminUtil.getPermissionByRoleId(roleId));
    }


    /**
     * 删除指定的分组下面的所有权限
     *
     * @param roleId
     * @return
     */
    @DeleteMapping("delete-role-permission-map-by-role-id")
    public ExResult<?> deleteRolePermissionMapDataByRoleId(@RequestParam("roleid") String roleId) {
        return ExResult.ofAuto(adminUtil.deleteRolePermissionMapDataByRoleId(roleId));
    }


    /**
     * 创建指定的分组编号的权限映射列表
     *
     * @param roleid
     * @param permissionList
     * @return
     * @throws AdminUserException
     */
    @PostMapping("create-role-permission-map-list")
    public ExResult<?> createRolePermissionMapData(@RequestParam("roleid") String roleid,
                                                   @RequestBody List<String> permissionList) throws AdminUserException {
        return ExResult.ofAuto(adminUtil.createRolePermissionMapDataByRoleId(roleid, permissionList));
    }
}
